Skip to content

Add type intersection expressions#948

Merged
dnwpark merged 13 commits into
masterfrom
when-type
Oct 21, 2025
Merged

Add type intersection expressions#948
dnwpark merged 13 commits into
masterfrom
when-type

Conversation

@dnwpark

@dnwpark dnwpark commented Oct 10, 2025

Copy link
Copy Markdown
Contributor

Given a schema:

type Inh_A { a: int64; };
type Inh_B { b: int64; };
type Inh_C { c: int64; };
type Inh_AB extending Inh_A, Inh_B { ab: int64; };
type Inh_AC extending Inh_A, Inh_C { ac: int64; };
type Inh_BC extending Inh_B, Inh_C { bc: int64; };
type Inh_ABC extending Inh_A, Inh_B, Inh_C { abc: int64; };
type Inh_AB_AC extending Inh_AB, Inh_AC { ab_ac: int64; };

Supports queries such as:

client.query(
    default.Inh_A.when_type(default.Inh_B).is_(default.Inh_C)
)
client.query(
    default.Inh_AB.select(a=lambda x: x.is_(default.Inh_C).c)
)

@dnwpark dnwpark force-pushed the when-type branch 9 times, most recently from c49b232 to fe02e9f Compare October 17, 2025 00:02
@dnwpark dnwpark marked this pull request as ready for review October 17, 2025 16:14
@msullivan

Copy link
Copy Markdown
Member

I think we need to call it is_ or is_type or something. I don't love when_type

type_: TypeNameExpr,
) -> None:
op = _edgeql.Token.RANGBRACKET
object.__setattr__(self, "type_filter", type_filter)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an issue for another PR (or not bothering), but all these __setattr__s and explicit __init__s in a dataclass are dodgy; are they all just so that op doesn't show up in the dataclass string like it would if it was declared with a default

@msullivan msullivan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to find a new name probably though

Comment on lines +303 to +306
def create_intersection(
lhs: _T_Lhs,
rhs: _T_Rhs,
) -> type[BaseGelModelIntersection[_T_Lhs, _T_Rhs]]:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should consider caching this, maybe?
Or at least, put in a note to consider caching it...

return result


def create_intersection(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments about what is being done here exactly would be useful

elif expr_object_types is not None:
self.cached_return_type = return_type
self.cached_return_type_proxy = None
worklist = list(expr_object_types)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe name this something different than worklist here, and make a copy to populate the initial worklist?
(Instead of needing to recreate the worklist conditionally after the DFS loop)

Comment on lines +87 to +90
@classmethod
def when_type(
cls: _T_SelfModel, /, other_model: _T_OtherModel
) -> type[BaseGelModelIntersection[_T_SelfModel, _T_OtherModel]]: ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should declare it as returning type[_T_OtherModel]?
That's probably the most useful thing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is that you still want to be able to select pointers from _T_SelfModel. For example:

default.Inh_A.when_type(default.Inh_B).select(a=True, b=True)

Though to be fair the type checking here is still busted...

@dnwpark dnwpark merged commit 17035e8 into master Oct 21, 2025
43 checks passed
@dnwpark dnwpark deleted the when-type branch October 21, 2025 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants